package com.wj.algorithm.lintcode.p552;

import java.util.Arrays;
import java.util.Random;

public class Solution {

    class ArrayBean {
        private int[] arr;
        private int len = 0;
        private int finalIndex = 0;

        private int currentIndex = -1;

        public ArrayBean(int[] arr) {
            super();
            this.arr = arr;
            this.len = arr.length;
            this.finalIndex = 0;
            if (this.len <= 0) {
                this.finalIndex = 0;
            }
        }

        public int[] getArr() {
            return arr;
        }

        public void setArr(int[] arr) {
            this.arr = arr;
        }

        public int getFinalIndex() {
            return finalIndex;
        }

        public void setFinalIndex(int finalIndex) {
            this.finalIndex = finalIndex;
        }

        public int getCurrentIndex() {
            return currentIndex;
        }

        public void setCurrentIndex(int currentIndex) {
            this.currentIndex = currentIndex;
        }

        public int getLen() {
            return len;
        }

        public void setLen(int len) {
            this.len = len;
        }

        public boolean isValid() {
            return finalIndex < len;
        }

        public void tryMoveToNextMax(int toIndex) {
            currentIndex = -1;
            int maxValue = -1;
            for (int i = finalIndex; i < len && i <= toIndex; i++) {
                if (arr[i] > maxValue) {
                    maxValue = arr[i];
                    this.currentIndex = i;
                }
            }
            //            this.finalIndex = 0;
        }

        public int moveToCurrent() {
            if (currentIndex >= 0) {
                finalIndex = currentIndex;
                int v = arr[finalIndex];
                finalIndex ++;
                return v;
            }
            return -1;
        }

        public int compareTo(ArrayBean b) {
            if (this.currentIndex > -1 && b.currentIndex > -1) {
                if (this.arr[currentIndex] > b.arr[b.currentIndex]) {
                    return 1;
                }
                if (this.arr[currentIndex] < b.arr[b.currentIndex]) {
                    return -1;
                }
                return 0;
            } else {
                if (this.currentIndex > -1) {
                    return 1;
                }
                if (b.currentIndex > -1) {
                    return -1;
                }
            }
            return 0;
        }
    }

    public int testEqual(ArrayBean m, ArrayBean n) {

        return -1;
    }

    public int[] maxNumber(int[] nums1, int[] nums2, int k) {
        if(k > nums1.length + nums2.length) {
            return new int[0];
        }
        return getMaxNumber(nums1, nums2, k);
    }

    public int compare(int[] a, int[] b) {
        if(a.length > b.length ) {
            return 1;
        } else if(a.length < b.length) {
            return -1;
        }
        
        for(int i = 0, len = a.length; i < len; i ++) {
            if(a[i] > b[i]) {
                return 1;
            } else if(a[i] < b[i]) {
                return -1;
            }
        }

        return 0;
    }
    
    public int[] combineMax(int[] nums1, int[] nums2, int k) {
        int[] rs = new int[k];

        ArrayBean m = new ArrayBean(nums1);
        ArrayBean n = new ArrayBean(nums2);
        int index = 0;
        while(index < k) {
            m.tryMoveToNextMax(m.finalIndex);
            n.tryMoveToNextMax(n.finalIndex);
            

            int compare = m.compareTo(n);
            if(compare > 0) {
                rs[index] = m.moveToCurrent();
                index ++;
            } else if(compare < 0) {
                rs[index] = n.moveToCurrent();
                index ++;
            } else {
                rs[index] = m.arr[m.currentIndex];

                index ++;
                int[] ifM = getMaxNumber(Arrays.copyOfRange(nums1, m.getCurrentIndex() + 1, m.len), Arrays.copyOfRange(nums2, n.finalIndex, n.len), k - index);
                int[] ifN = getMaxNumber(Arrays.copyOfRange(nums1, m.finalIndex, m.len), Arrays.copyOfRange(nums2, n.getCurrentIndex() + 1, n.len), k - index);
                if (compare(ifM, ifN) > 0) {
                    System.arraycopy(ifM, 0, rs, index, ifM.length);
                    return rs;
                } else {
                    System.arraycopy(ifN, 0, rs, index, ifM.length);
                    return rs;
                }
            }
        }
        return rs;
    }
    static long count = 0;
    public int[] getMaxNumber(int[] nums1, int[] nums2, int k) {
//        String key = nums1.length + "_" + nums2.length + "_" + k;

        System.out.println(++ count);
        if(nums1.length + nums2.length < k) {
            return new int[0];
        }
        if(nums1.length + nums2.length == k) {
            return combineMax(nums1, nums2, k);
        }
//        System.out.println(key);
        
        ArrayBean m = new ArrayBean(nums1);
        ArrayBean n = new ArrayBean(nums2);
        int[] rs = new int[k];
        int kIndex = 0;
        while (kIndex < k && (m.isValid() || n.isValid())) {
            m.tryMoveToNextMax(m.getLen() + n.getLen() - n.finalIndex - (k - kIndex));
            n.tryMoveToNextMax(m.getLen() + n.getLen() - m.finalIndex - (k - kIndex));

            int compare = m.compareTo(n);
            if (compare > 0) {
                rs[kIndex] = m.moveToCurrent();
                kIndex++;
            } else if (compare < 0) {
                rs[kIndex] = n.moveToCurrent();
                kIndex++;
            } else {
                rs[kIndex] = m.arr[m.currentIndex];

                kIndex++;
                int[] ifM = getMaxNumber(Arrays.copyOfRange(nums1, m.getCurrentIndex() + 1, m.len), Arrays.copyOfRange(nums2, n.finalIndex, n.len), k - kIndex);
                int[] ifN = getMaxNumber(Arrays.copyOfRange(nums1, m.finalIndex, m.len), Arrays.copyOfRange(nums2, n.getCurrentIndex() + 1, n.len), k - kIndex);
                if (compare(ifM, ifN) > 0) {
                    System.arraycopy(ifM, 0, rs, kIndex, ifM.length);
                    return rs;
                } else {
                    System.arraycopy(ifN, 0, rs, kIndex, ifM.length);
                    return rs;
                }
            }
        }
        return rs;
    }
    
    public static int[] random(int n) {
        int[] rs = new int[n];
        
        for(int i = 0; i < n; i ++) {
            Random r = new Random(System.currentTimeMillis() + i);
            rs[i] = r.nextInt(10);
        }
        
        return rs;
    }

    public static void showArra(int[] arr) {
        for (int a : arr) {
            System.out.print(a + " ");
        }
    }
    public static void main(String[] args) {
        //        //
//        int[] m = new int[] { 3, 9 };
//        int[] n = new int[] { 8, 9 };

        int mRandom = 0;
        int nRandom = 0;
        int[] m = new int[] { 5,0,2,1,0,1,0,3,9,1,2,8,0,9,8,1,4,7,3 };
        int[] n = new int[] { 7,6,7,1,0,1,0,5,6,0,5,0 };
        
//        m = new int[] { 5,6,6 };
//        n = new int[] { 5,6,5 };
        
        mRandom = m.length;
        nRandom = n.length;

//        m = new int[]{8,9,7,3,5,9,1,0,8,5,3,0,9,2,7,4,8,9,8,1,0,2,0,2,7,2,3,5,4,7,4,1,4,0,1,4,2,1,3,1,5,3,9,3,9,0,1,7,0,6,1,8,5,6,6,5,0,4,7,2,9,2,2,7,6,2,9,2,3,5,7,4,7,0,1,8,3,6,6,3,0,8,5,3,0,3,7,3,0,9,8,5,1,9,5,0,7,9,6,8,5,1,9,6,5,8,2,3,7,1,0,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2,6,8,1,9,7,8,8,9,5,1,1,4,2,0,1,3,6,7,8,7,0,5,6,0,1,7,9,6,4,8,6,7,0,2,3,2,7,6,0,5,0,9,0,3,3,8,5,0,9,3,8,0,1,3,1,8,1,8,1,1,7,5,7,4,1,0,0,0,8,9,5,7,8,9,2,8,3,0,3,4,9,8,1,7,2,3,8,3,5,3,1,4,7,7,5,4,9,2,6,2,6,4,0,0,2,8,3,3,0,9,1,6,8,3,1,7,0,7,1,5,8,3,2,5,1,1,0,3,1,4,6,3,6,2,8,6,7,2,9,5,9,1,6,0,5,4,8,6,6,9,4,0,5,8,7,0,8,9,7,3,9,0,1,0,6,2,7,3,3,2,3,3,6,3,0,8,0,0,5,2,1,0,7,5,0,3,2,6,0,5,4,9,6,7,1,0,4,0,9,6,8,3,1,2,5,0,1,0,6,8,6,6,8,8,2,4,5,0,0,8,0,5,6,2,2,5,6,3,7,7,8,4,8,4,8,9,1,6,8,9,9,0,4,0,5,5,4,9,6,7,7,9,0,5,0,9,2,5,2,9,8,9,7,6,8,6,9,2,9,1,6,0,2,7,4,4,5,3,4,5,5,5,0,8,1,3,8,3,0,8,5,7,6,8,7,8,9,7,0,8,4,0,7,0,9,5,8,2,0,8,7,0,3,1,8,1,7,1,6,9,7,9,7,2,6,3,0,5,3,6,0,5,9,3,9,1,1,0,0,8,1,4,3,0,4,3,7,7,7,4,6,4,0,0,5,7,3,2,8,5,1,4,5,8,5,6,7,5,7,3,3,9,6,8,1,5,1,1,1,0,3};

//        mRandom = 500;
//        nRandom = 533;
//        m = random(mRandom);
//        n = random(nRandom);
        long b = System.currentTimeMillis();
        int[] rs = new Solution().maxNumber(m, n, mRandom + nRandom);
        b = System.currentTimeMillis() - b;
        
        showArra(rs);
        System.out.println();
        System.out.println(b);
    }
}
